<?php
declare (strict_types = 1);

namespace app\controller\api\v1\user\scale;

use app\controller\api\ApiController;
use app\exception\LogicException;
use app\model\communal\ScaleMultiModel;
use app\service\logic\common\maintain\BannerService;
use app\service\logic\common\order\OrderMultiService;
use app\service\logic\common\scale\MultiService;
use app\validate\common\scale\MultiValidate;
use think\App;
use think\response\Json;
use think\db\exception\DbException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class MultiController extends ApiController
{
    protected $service;

    public function __construct(App $app, MultiService $service)
    {
        parent::__construct($app);

        $this->service = $service;
    }

    /**
     * 心里测试banner
     *
     * @return Json
     */
    public function banner()
    {
        $result = invoke(BannerService::class)
            ->getList([], ['client_type', 'state'], ['client_type' => 3, 'state' => 1], [], null, false);
        return amis($result);
    }

    /**
     * 测试列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, MultiValidate::class . '.index');
        $search['state'] = 1;

        $result = $this->service->getList($params, $search_fields, $search);
        return amis($result);
    }

    /**
     * 下拉菜单
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function options(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);
        $search['state'] = 1;

        $drop = $this->service->getOptions($params, $search_fields, $search);
        return amis(['options' => $drop]);
    }

    /**
     * 每日推荐
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function recommend()
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, MultiValidate::class . '.index');
        $search['state'] = 1;
        $search['is_recommend'] = 1;

        $result = $this->service->getList($params, $search_fields, $search);
        return amis($result);
    }

    /**
     * 精选测评
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function featured()
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, MultiValidate::class . '.index');
        $search['state'] = 1;
        $search['is_featured'] = 1;

        $result = $this->service->getList($params, $search_fields, $search);
        return amis($result);
    }

    /**
     * 筛选 测试价格选项
     *
     * @return Json
     */
    public function price()
    {
        $options = $this->service->amisOptions(ScaleMultiModel::$priceOptions);
        return amis(['options' => $options]);
    }

    /**
     * 筛选 排序选项
     *
     * @return Json
     */
    public function order()
    {
        $options = $this->service->amisOptions(ScaleMultiModel::$orderOptions);
        return amis(['options' => $options]);
    }

    /**
     * 详情
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read()
    {
        $params = $this->request->only(['id']);
        $this->validate($params, MultiValidate::class . '.read');

        $fun = function ($item) {
            unset($item['belongCategory']);
            return $item;
        };
        $result = $this->service->read($params, ['belongCategory'], $fun);
        return amis($result);
    }

    /**
     * 付款
     *
     * @return Json
     */
    public function payment()
    {
        $params = $this->request->only(['id', 'payment_type', 'is_coin']);
        $this->validate($params, MultiValidate::class . '.payment');
        $user = $this->request->middleware('user');
        $params['user_id'] = $user['id'];

        // 创建待支付订单
        $order = invoke(OrderMultiService::class)->create($params);
        // 支付订单
        $payment = invoke(OrderMultiService::class)->payment($order);
        // $payment = invoke(OrderMultiService::class)->paymentPage($order); // debug用 alipay
        return amis([
            'order' => $order,
            'payment' => $payment,
        ]);
    }
}
